/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wiflydemo_spotserver_tcp_ondesktop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author JaredSmith
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        //Port number that your computer will be listening on
        int port = 8000;
        ServerSocket serverSocket = null;
        Socket socket = null;

        String data = null;

        System.out.println("Making Socket Server.");
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 1234.");
            System.exit(1);
        }
        java.net.InetAddress address = java.net.InetAddress.getLocalHost();

        System.out.println(address.toString());
        System.out.println("Accepting Socket");
        /**
         * You will know if you have not configured the WiFly properly because
         * the code will hang here until it connects.
         */
        try {
            socket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        System.out.println("Making input stream.");
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));


        System.out.println("### hostname/ip " + address); // name and IP address
        System.out.println("### hostname " + address.getHostName()); // name
        System.out.println("### ip " + address.getHostAddress()); // IP address
        System.out.println("### port " + port);

        //Once connected to the WiFly, loop and print out the data
        while(true) {
            try {
                data = in.readLine();
                System.out.println("Light Intensity = " + data);
            } catch(IOException e) {

            }
        }
    }

}